﻿using LdtDataEngin.ModuleDetail.PersonnelAnalysis.PersonnelModel.Model;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.Personnel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LdtDataEngin.ModuleDetail.PersonnelAnalysis.PersonnelModel.Personnel
{
    public class HandleBaseClass
    {
        public DataContext dataContext;
        #region 基础信息
        /// <summary>
        ///参数内容
        /// </summary>
        public TableModel tableModel;
        /// <summary>
        /// 当前人员代称信息
        /// </summary>
        public string RowRelatRoleName { get; set; }

        /// <summary>
        /// 人员信息
        /// </summary>
        public ElementSpokeName spoke { get; set; }
        #endregion

        #region 刑事公诉人处理方法
        /// <summary>
        /// 处理公诉人问题
        /// </summary>
        public void Prosecution()
        {
            if (dataContext.DataLayerAns.CaseType == "a1")
            {
                tableModel.spokeName.ES_ShortName = "公";
                if (tableModel.spokeNameList.ContainsKey(RowRelatRoleName))
                {
                    ElementSpokeName elementname = tableModel.spokeNameList[RowRelatRoleName].FirstOrDefault(a => a.ES_ShortName == RowRelatRoleName);
                    tableModel.spokeName.ES_ShortName = elementname.ES_ShortName;
                }
                #region 公诉人整理代码
                //if (this.DictAllSpokeMan.ContainsKey(RowRelatRoleName + "公"))
                //{
                //    this.DictAllSpokeMan[RowRelatRoleName + "公"] = mspokeman;
                //}
                //else if (this.DictAllSpokeMan.ContainsKey(RowRelatRoleName + "公诉人"))
                //{
                //    mspokeman.spokeName = "公诉人";
                //    this.DictAllSpokeMan[RowRelatRoleName + "公诉人"] = mspokeman;
                //}
                //this.DictAllSpokeMan.Add(RowRelatRoleName + mspokeman.fullName, mspokeman);
                //if (!this.DictRoleSpokemen.ContainsKey(RowRelatRoleName))
                //{
                //    this.DictRoleSpokemen.Add(RowRelatRoleName, new List<TrialMC_Spokeman>() { mspokeman });
                //}
                //else
                //{
                //    this.DictRoleSpokemen[RowRelatRoleName].Add(mspokeman);
                //}
                #endregion
            }
        }
        #endregion

        #region 代理人处理

        /// <summary>
        /// 一审代理人代称处理问题
        /// </summary>
        public string FirstInstanceOthen(string suidegreeName, string casetype)
        {

            string suidegreename = tableModel.spokeName.ES_SuitDegreeName;
            string sp_id = tableModel.spokeName.Sp_id;
            ElementSpokeName elementSpoke = dataContext.DataLayerAns.ElementSpokeList.FirstOrDefault(a => a.Sp_id == tableModel.spokeName.ES_AssertorID);
            if (elementSpoke != null)
            {
                string Pre = "";//前缀
                string Mid = "";//中缀
                string name = "";//当事人姓名
                string spIndex = "";//序号;
                string prname = "";//代理人姓名
                name = elementSpoke.ES_FullName;
                string suitdegreeid = elementSpoke.ES_SuitDrgreeID;
                string participanttypeid = elementSpoke.Sp_participanttypeid;
                spIndex = elementSpoke.ES_RoleIndex;
                prname = tableModel.spokeName.ES_FullName;

                if (suidegreeName != "负责人")
                {
                    if (suidegreeName != "法定代理人")
                    {
                        if (suitdegreeid == "89")
                        {
                            Pre = "原";
                            Mid = "保";
                        }
                        else if (suitdegreeid == "90" || suitdegreeid == "1")
                        {

                            if (participanttypeid == "t1")
                            {
                                Pre = "被";
                                Mid = "";
                            }
                            else
                            {
                                if (name.IndexOf("保险") > 0)
                                {
                                    Pre = "被";
                                    Mid = "保";
                                }
                                else
                                {
                                    Pre = "被";
                                    Mid = "公";
                                }
                            }
                        }
                    }
                    try
                    {
                        spoke = tableModel.spokeNameList[RowRelatRoleName].Where(a => true && tableModel.determine.IsMatchProxy(Pre, Mid, name, spIndex, a, prname)).FirstOrDefault();
                    }
                    catch (Exception)
                    {

                    }

                    if (spoke != null)
                    {
                        SaveSpokeName(spoke);
                        UpdateSpokeMan(spoke.ES_ShortName);
                        return spoke.ES_ShortName;
                    }
                    else
                    {
                        if (tableModel.spokeNameList.ContainsKey("undefined"))
                        {
                            if ("89".IndexOf(tableModel.spokeName.ES_SuitDrgreeID) >= 0)
                            {
                                Pre = "原";
                                Mid = "保";
                                spoke = tableModel.spokeNameList["undefined"].Where(a => true && tableModel.determine.IsMatchProxy(Pre, Mid, name, spIndex, a, prname)).FirstOrDefault();
                                if (spoke == null)
                                {
                                    Pre = "原告";
                                    Mid = "";
                                }
                            }
                            else if ("90".IndexOf(tableModel.spokeName.ES_SuitDrgreeID) >= 0)
                            {
                                Pre = "被";
                                Mid = "保";
                                spoke = tableModel.spokeNameList["undefined"].Where(a => true && tableModel.determine.IsMatchProxy(Pre, Mid, name, spIndex, a, prname)).FirstOrDefault();
                                if (spoke == null && tableModel.spokeName.Sp_participanttypeid == "t1")
                                {
                                    Pre = "被告";
                                    Mid = "";
                                }
                                else
                                {
                                    Pre = "被";
                                    Mid = "公";
                                }
                            }
                            else if (tableModel.spokeName.ES_SuitDrgreeID == "1" && tableModel.spokeName.Sp_participanttypeid == "t1")
                            {
                                Pre = "被";
                                Mid = "";
                            }

                            if (spoke == null)
                            {
                                spoke = tableModel.spokeNameList["undefined"].Where(a => true && tableModel.determine.IsMatchProxy(Pre, Mid, name, spIndex, a, prname)).FirstOrDefault();
                            }
                            if (spoke != null)
                            {
                                SaveSpokeName(spoke);
                                UpdateSpokeMan(spoke.ES_ShortName);
                                return spoke.ES_ShortName;
                            }
                            else
                            {
                                return "";
                            }
                        }
                    }
                }
            }

            return "";
        }

        /// <summary>
        /// 二审代理人代称处理
        /// </summary>
        public string SecondInstanceOthen(string suidegreeName, string casetype)
        {
            string suidegreename = tableModel.spokeName.ES_SuitDegreeName;
            string sp_id = tableModel.spokeName.Sp_id;
            //Dictionary<string, string> dicSp_table = dytable.DicExtTable["cs_suitparticipant"].FirstOrDefault(a => a["sp_id"] == sp_id);

            //string Pre = "";//前缀
            //string Mid = "";//中缀
            //string name = "";//当事人姓名
            //string spIndex = "";//序号;
            //string prname = "";//代理人姓名
            //name = dicSp_table["sp_name"];
            //spIndex = dicSp_table["sp_sid"];
            //prname = tableModel.spokeName.ES_FullName;

            ElementSpokeName elementSpoke = dataContext.DataLayerAns.ElementSpokeList.FirstOrDefault(a => a.Sp_id == tableModel.spokeName.ES_ID);
            string Pre = "";//前缀
            string Mid = "";//中缀
            string name = "";//当事人姓名
            string spIndex = "";//序号;
            string prname = "";//代理人姓名
            //name = dicSp_table["sp_name"];
            //spIndex = dicSp_table["sp_sid"];
            name = elementSpoke.ES_FullName;
            spIndex = elementSpoke.ES_RoleIndex;
            prname = tableModel.spokeName.ES_FullName;

            if (suidegreeName != "负责人")
            {
                if (suidegreeName != "法定代理人")
                {
                    //代理人信息可以匹配出来的情况

                    if ("7、94、95、96、97、98".IndexOf(tableModel.spokeName.ES_SuitDrgreeID) >= 0)
                    {
                        Pre = "上诉人";
                        Mid = "保";
                    }
                    else if ("13、99、100、101、102、103".IndexOf(tableModel.spokeName.ES_SuitDrgreeID) >= 0 || tableModel.spokeName.ES_SuitDrgreeID == "1")
                    {

                        if (tableModel.spokeName.Sp_participanttypeid == "t1")
                        {
                            Pre = "被上诉";
                            Mid = "";
                        }
                        else
                        {
                            if (tableModel.spokeName.ES_FullName.IndexOf("保险") > 0)
                            {
                                Pre = "被上诉";
                                Mid = "保";
                            }
                            else
                            {
                                Pre = "被上诉";
                                Mid = "公";
                            }
                        }
                    }
                }
                spoke = tableModel.spokeNameList[RowRelatRoleName].Where(a => true && tableModel.determine.IsMatchProxy(Pre, Mid, name, spIndex, a, prname)).FirstOrDefault();
                if (spoke != null)
                {
                    SaveSpokeName(spoke);
                    return spoke.ES_ShortName;
                }
                else
                {
                    if (tableModel.spokeNameList.ContainsKey("undefined"))
                    {
                        if ("7、94、95、96、97、98".IndexOf(tableModel.spokeName.ES_SuitDrgreeID) >= 0)
                        {
                            Pre = "上诉";
                            Mid = "保";
                        }
                        else if ("13、99、100、101、102、103".IndexOf(tableModel.spokeName.ES_SuitDrgreeID) >= 0)
                        {
                            if (tableModel.spokeName.Sp_participanttypeid == "t1")
                            {
                                Pre = "被上诉人";
                                Mid = "";
                            }
                            else
                            {
                                if (name.IndexOf("保险") > 0)
                                {
                                    Pre = "被上诉";
                                    Mid = "保";
                                }
                                else
                                {
                                    Pre = "被上诉";
                                    Mid = "公";
                                }
                            }
                        }

                        if (spoke == null)
                        {
                            spoke = tableModel.spokeNameList["undefined"].Where(a => true && tableModel.determine.IsMatchProxy(Pre, Mid, name, spIndex, a, prname)).FirstOrDefault();
                        }
                        if (spoke != null)
                        {
                            SaveSpokeName(spoke);
                            UpdateSpokeMan(spoke.ES_ShortName);
                            return spoke.ES_ShortName;
                        }
                        else
                        {
                            return "";
                        }
                    }
                }
            }
            return "";
        }


        #endregion

        #region 当事人处理

        /// <summary>
        /// 一审当事人代称处理
        /// </summary>
        /// <param name="suidegreeName"></param>
        /// <param name="casetype"></param>
        /// <returns></returns>
        public string FirstInstancePant(string suidegreeName, string casetype)
        {
            string suidegreename = tableModel.spokeName.ES_SuitDegreeName;
            string sp_id = tableModel.spokeName.Sp_id;
            //Dictionary<string, string> dicSp_table = dytable.DicExtTable["cs_suitparticipant"].FirstOrDefault(a => a["sp_id"] == tableModel.spokeName.ES_ID);
            //string Pre = "";//前缀
            //string name = "";//当事人姓名
            //string spIndex = "";//序号;
            //string prname = "";//代理人姓名
            //name = dicSp_table["sp_name"];
            //spIndex = dicSp_table["sp_sid"];
            //prname = tableModel.spokeName.ES_FullName;


            ElementSpokeName elementSpoke = dataContext.DataLayerAns.ElementSpokeList.FirstOrDefault(a => a.Sp_id == tableModel.spokeName.ES_ID);
            string Pre = "";//前缀
            string name = "";//当事人姓名
            string spIndex = "";//序号;
            string prname = "";//代理人姓名
            //name = dicSp_table["sp_name"];
            //spIndex = dicSp_table["sp_sid"];
            name = elementSpoke.ES_FullName;
            spIndex = elementSpoke.ES_RoleIndex;
            prname = tableModel.spokeName.ES_FullName;

            if (RowRelatRoleName == "Prosecutor")
            {
                //原告处理
                Pre = "原";
            }
            else if (RowRelatRoleName == "Accused")
            {
                //被告处理
                Pre = "被";
            }
            try
            {
                spoke = tableModel.spokeNameList[RowRelatRoleName].Where(a => true && tableModel.determine.IsMatchAccusedOrProsecutor(Pre, name, spIndex, a)).FirstOrDefault();
            }
            catch (Exception)
            {

                string bb = "1";
            }

            if (spoke != null)
            {
                SaveSpokeName(spoke);
                UpdateSpokeMan(spoke.ES_ShortName);
                return spoke.ES_ShortName;
            }
            else
            {
                //多个结果集
                List<ElementSpokeName> elementSpokesList = new List<ElementSpokeName>();
                //执行undfind
                if (tableModel.spokeNameList.ContainsKey("undefined"))
                {
                    elementSpokesList = (List<ElementSpokeName>)tableModel.spokeNameList["undefined"].Where(a => true && tableModel.determine.IsMatchAccusedOrProsecutor(prname, name, spIndex.ToString(), a)).ToList();

                    if (RowRelatRoleName == "Prosecutor")
                    {
                        //原告处理
                        if (elementSpokesList.Count > 0)
                        {
                            spoke = elementSpokesList[0];
                        }
                        if (spoke != null)
                        {
                            if (spoke.ES_ShortName.IndexOf("代") >= 0)
                            {
                                spoke = null;
                            }
                        }
                    }
                    else if (RowRelatRoleName == "Accused")
                    {
                        //被告处理
                        if (elementSpokesList.Count > 0)
                        {
                            spoke = elementSpokesList[0];
                        }
                        if (spoke != null)
                        {
                            if (spoke.ES_ShortName.IndexOf("代") >= 0)
                            {
                                spoke = null;
                            }
                        }
                    }
                    if (spoke != null)
                    {
                        SaveSpokeName(spoke);
                        UpdateSpokeMan(spoke.ES_ShortName);
                        return spoke.ES_ShortName;
                    }
                    else
                    {
                        return "";
                    }
                }
            }
            return "";
        }

        /// <summary>
        /// 二审当事人代称处理
        /// </summary>
        /// <param name="suidegreeName"></param>
        /// <param name="casetype"></param>
        /// <returns></returns>
        public string SecondInstancePant(string suidegreeName, string casetype)
        {
            string suidegreename = tableModel.spokeName.ES_SuitDegreeName;
            string sp_id = tableModel.spokeName.Sp_id;
            //Dictionary<string, string> dicSp_table = dytable.DicExtTable["cs_suitparticipant"].FirstOrDefault(a => a["sp_id"] == tableModel.spokeName.ES_ID);
            ElementSpokeName elementSpoke = dataContext.DataLayerAns.ElementSpokeList.FirstOrDefault(a => a.Sp_id == tableModel.spokeName.ES_ID);
            string Pre = "";//前缀
            string name = "";//当事人姓名
            string spIndex = "";//序号;
            string prname = "";//代理人姓名
            //name = dicSp_table["sp_name"];
            //spIndex = dicSp_table["sp_sid"];
            name = elementSpoke.ES_FullName;
            spIndex = elementSpoke.ES_RoleIndex;
            prname = tableModel.spokeName.ES_FullName;

            if (RowRelatRoleName == "appellant")
            {
                //上诉人处理
                Pre = "上诉";
            }
            else if (RowRelatRoleName == "appellee")
            {
                //被上诉人处理
                Pre = "被上诉";
            }
            spoke = tableModel.spokeNameList[RowRelatRoleName].Where(a => true && tableModel.determine.IsMatchAccusedOrProsecutor(Pre, name, spIndex, a)).FirstOrDefault();
            if (spoke != null)
            {
                SaveSpokeName(spoke);
                UpdateSpokeMan(spoke.ES_ShortName);
                return spoke.ES_ShortName;
            }
            else
            {
                //多个结果集
                List<ElementSpokeName> elementSpokesList = new List<ElementSpokeName>();
                //执行undfind
                if (tableModel.spokeNameList.ContainsKey("undefined"))
                {
                    elementSpokesList = (List<ElementSpokeName>)tableModel.spokeNameList["undefined"].Where(a => true && tableModel.determine.IsMatchAccusedOrProsecutor(prname, name, spIndex.ToString(), a)).ToList();

                    if (RowRelatRoleName == "appellant")
                    {
                        //上诉人处理
                        if (elementSpokesList.Count > 0)
                        {
                            spoke = elementSpokesList[0];
                        }
                        if (spoke != null)
                        {
                            if (spoke.ES_ShortName.IndexOf("代") >= 0)
                            {
                                spoke = null;
                            }
                        }
                    }
                    else if (RowRelatRoleName == "appellee")
                    {
                        //被上诉人处理
                        if (elementSpokesList.Count > 0)
                        {
                            spoke = elementSpokesList[0];
                        }
                        if (spoke != null)
                        {
                            if (spoke.ES_ShortName.IndexOf("代") >= 0)
                            {
                                spoke = null;
                            }
                        }
                    }
                    if (spoke != null)
                    {
                        SaveSpokeName(spoke);
                        UpdateSpokeMan(spoke.ES_ShortName);
                        return spoke.ES_ShortName;
                    }
                    else
                    {
                        return "";
                    }
                }
            }
            return "";
        }

        #endregion

        #region 审判人员信息

        /// <summary>
        /// 审判人员信息
        /// </summary>
        /// <returns></returns>
        public string Trial()
        {
            if (!string.IsNullOrEmpty(RowRelatRoleName))
            {
                spoke = tableModel.spokeNameList[RowRelatRoleName].FirstOrDefault();
                if (spoke != null)
                {
                    SaveSpokeName(tableModel.spokeName);
                    UpdateSpokeMan(spoke.ES_ShortName);
                    return spoke.ES_ShortName;
                }
            }
            else
            {
                tableModel.spokeNameList["Judger"].Add(tableModel.spokeName);
            }
            return null;
        }

        #endregion

        #region 基础处理方法
        /// <summary>
        /// 保存人员代称数据
        /// </summary>
        /// <param name="elementSpokeName"></param>
        public void SaveSpokeName(ElementSpokeName elementSpokeName)
        {
            if (string.IsNullOrEmpty(elementSpokeName.ES_FullName) || elementSpokeName.ES_FullName == tableModel.spokeName.ES_FullName)
            {
                string spokename = elementSpokeName.ES_ShortName;
                elementSpokeName = tableModel.spokeName;
                elementSpokeName.ES_ShortName = spokename;
            }
            else
            {
                string spokename = elementSpokeName.ES_ShortName;
                elementSpokeName = tableModel.spokeName;
                elementSpokeName.ES_ShortName = spokename;
            }
        }

        /// <summary>
        /// 更新数据集
        /// </summary>
        /// <param name="name"></param>
        public void UpdateSpokeMan(string name)
        {
            if (name != null)
            {
                if (tableModel.spokeNameList.ContainsKey("undefined"))
                {
                    ElementSpokeName ename = tableModel.spokeNameList["undefined"].FirstOrDefault(a => a.ES_ShortName == name);
                    this.tableModel.spokeNameList["undefined"].Remove(ename);
                }
                if (tableModel.spokeNameList.ContainsKey(RowRelatRoleName))
                {
                    ElementSpokeName ename = tableModel.spokeNameList[RowRelatRoleName].FirstOrDefault(a => a.ES_ShortName == name);
                    this.tableModel.spokeNameList[RowRelatRoleName].Remove(ename);
                }
            }

            StringBuilder returnStr = new StringBuilder();

            foreach (var item in tableModel.spokeNameList)
            {
                var query = (from ElementSpokeName item2 in item.Value where !string.IsNullOrEmpty(item2.ES_FullName) select item2.ES_ShortName).ToList();
                String allSpokeStr = "";
                if (query.Count > 1)
                {
                    allSpokeStr = String.Join("|", query);
                }
                else if (query.Count == 1)
                {
                    allSpokeStr = query.First();
                }

                returnStr.Append(item.Key + ":" + allSpokeStr + ",");
            }
            //_SpokeManShortName = returnStr.ToString();
        }
        #endregion

    }
}
